package com.sav.common.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

import com.sav.common.catalog.*;
import com.sav.common.localCatalog.Catalog;
import com.sav.common.localCatalog.LocalCategory;

@Component
public class CategoryDAO extends BaseDAO {
		
	public CategoryDAO() {
	}
	
	public void saveChanges(Catalog c) {
		List<LocalCategory> list = c.getListParentCategory();
		for (LocalCategory pc : list) {
			saveLocalCategory(pc);
		}
		
		for (Integer id : c.getListDeleted()) {
			deleteCategory(id);
		}
	}
	
	public void saveLocalCategory(LocalCategory localCategory) {
		PreparedStatement stm = null;
		ResultSet resultSet = null;
		boolean exists = false;
		try {
			String query = "SELECT * FROM category WHERE idCategory = ?";
			stm = getConnection().prepareStatement(query);
			stm.setInt(1, localCategory.getIdCategory());
			resultSet = stm.executeQuery();
			if (resultSet != null) exists = resultSet.next();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				stm.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		try {
			if (exists) {
				String query = "UPDATE category SET name = ?, description = ?, orderNum = ? WHERE idCategory = ?";
				stm = getConnection().prepareStatement(query);
				stm.setString(1, localCategory.getName());
				stm.setString(2, localCategory.getDescription());
				stm.setInt(3, localCategory.getOrderNum());
				stm.setInt(4, localCategory.getIdCategory());
				
				stm.executeUpdate();
			} else {
				String query = "INSERT INTO category SET idCategory = ?, name = ?, description = ?, orderNum = ?, idParentCategory = ?";
				stm = getConnection().prepareStatement(query);
				stm.setInt(1, localCategory.getIdCategory());
				stm.setString(2, localCategory.getName());
				stm.setString(3, localCategory.getDescription());
				stm.setInt(4, localCategory.getOrderNum());
				if (localCategory.getParentCategory() == null) 
					stm.setNull(5, java.sql.Types.NULL); else
						stm.setInt(5, localCategory.getParentCategory().getIdCategory());
				
				stm.executeUpdate();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				stm.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		for (LocalCategory lc : localCategory.getListSubCategory()) {
			saveLocalCategory(lc);
		}
	}
	
	public void changeCategory(int id, String name, String description) {
		PreparedStatement stm = null;
		try {
			String query = "UPDATE category SET name = ?, description = ? WHERE idCategory = ?";
			stm = getConnection().prepareStatement(query);
			stm.setString(1, name);
			stm.setString(2, description);
			stm.setInt(3, id);
			stm.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				stm.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	public int addCategory(String name, String description, Integer idParent) {
		
		String idParentCategory;
		String str;
		if (idParent == 0) {
			idParentCategory = "NULL";
			str = "is NULL";
			
		} else {
			idParentCategory = idParent.toString();
			str = "=" + idParentCategory;
		}
		
		int maxOrderNum = 0;
		int id = 0;
		ResultSet resultSet;
		
		PreparedStatement stm = null;
		try {
			String query = "SELECT MAX(orderNum) as m FROM category WHERE idParentCategory " + str; 
			stm = getConnection().prepareStatement(query);
			resultSet = stm.executeQuery();
			resultSet.next();
			maxOrderNum = resultSet.getInt("m");
			maxOrderNum++;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				stm.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		String query = "INSERT INTO category(idParentCategory, name, description, orderNum) VALUES(?, ?, ?, ?)";
		stm =  createStatementForInsert(query);
		try {
			if (idParent != 0) stm.setInt(1, idParent); else stm.setNull(1, java.sql.Types.NULL);
			stm.setString(2, name);
			stm.setString(3, description);
			stm.setInt(4, maxOrderNum);
			id = executeInsert(stm);
		} catch (SQLException e1) {
			e1.printStackTrace();
		} finally {
			try {
				stm.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		return id;
	}
	
	public void addCategory(Integer idParent, String name, String description, Integer orderNum) {
		
	/*	String idParentCategory;
		//String str;
		if (idParent == 0) {
			idParentCategory = "NULL";
		//	str = "is NULL";
			
		} else {
			idParentCategory = idParent.toString();
		//	str = "=" + idParentCategory;
		} */
		
		
		PreparedStatement stm = null;
		
		String query = "INSERT INTO category(idParentCategory, name, description, orderNum) VALUES(?, ?, ?, ?)";
		stm =  createStatementForInsert(query);
		try {
			if (idParent != 0) stm.setInt(1, idParent); else stm.setNull(1, java.sql.Types.NULL);
			stm.setString(2, name);
			stm.setString(3, description);
			stm.setInt(4, orderNum);
			executeInsert(stm);
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
	}
	
	
		
	public List<Category> getListCategory() {
		ArrayList<Category> list = new ArrayList<Category>();
		PreparedStatement stm = null;
		try {
			String query = "SELECT * FROM category ORDER BY orderNum";
			stm = getConnection().prepareStatement(query);
			ResultSet resultSet = stm.executeQuery();
			while (resultSet.next()) {
				int idCategory = resultSet.getInt("idCategory");
				int idParentCategory = resultSet.getInt("idParentCategory");
				String name = resultSet.getString("name");
				String description = resultSet.getString("description");
				int orderNum = resultSet.getInt("orderNum");
				list.add(new Category(idCategory, idParentCategory, name, description, orderNum));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				stm.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		return list;
	}
		
		
	public List<Category> getListSubCategory(int id) {
		ArrayList<Category> list = new ArrayList<Category>();
		PreparedStatement stm = null;
		try {
			String query = "SELECT * FROM category WHERE idParentCategory = ? ORDER BY orderNum";
			stm = getConnection().prepareStatement(query);
			stm.setInt(1, id);
			ResultSet resultSet = stm.executeQuery();
			
			while (resultSet.next()) {
				int idCategory = resultSet.getInt("idCategory");
				int idParentCategory = resultSet.getInt("idParentCategory");
				String name = resultSet.getString("name");
				String description = resultSet.getString("description");
				int orderNum = resultSet.getInt("orderNum");
				list.add(new Category(idCategory, idParentCategory, name, description, orderNum));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				stm.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		return list;
	}
		
	public List<Category> getListParentCategory() {
		ArrayList<Category> list = new ArrayList<Category>();
		PreparedStatement stm = null;
		try {
			String query = "SELECT * FROM category WHERE idParentCategory is NULL  ORDER BY orderNum";
		//	stm = getConnection().prepareStatement(query);
			stm = getConnection().prepareStatement(query);
			ResultSet resultSet = stm.executeQuery();
			while (resultSet.next()) {
				int idCategory = resultSet.getInt("idCategory");
				int idParentCategory = resultSet.getInt("idParentCategory");
				String name = resultSet.getString("name");
				String description = resultSet.getString("description");
				int orderNum = resultSet.getInt("orderNum");
				list.add(new Category(idCategory, idParentCategory, name, description, orderNum));
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				stm.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		return list;
	}
		
	public int getCountProductsInCategory(int id) {
		int count = 0;
		PreparedStatement stm = null;
		try {
			String query = "SELECT COUNT(idProduct) as c FROM products, category WHERE "
					+ "products.idCategory=category.idCategory and category.idCategory = ?";
			stm = getConnection().prepareStatement(query);
			stm.setInt(1, id);
			ResultSet resultSet = stm.executeQuery();
			resultSet.next();
			count = resultSet.getInt("c");
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				stm.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
			
		return count;
	}
	
	public boolean haveSubCategory(int id) {
		PreparedStatement stm = null;
		int count = 0;
		try {
			String query = "SELECT COUNT(idCategory) as c FROM category WHERE category.idParentCategory = ?";
			stm = getConnection().prepareStatement(query);
			stm.setInt(1, id);
			ResultSet resultSet = stm.executeQuery();
			resultSet.next();
			count = resultSet.getInt("c");
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				stm.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		if (count == 0) 
			return false; else 
				return true;
	}
	
	public Category getCategoryByID(int id) {
		PreparedStatement stm = null;
		try {
			String query = "SELECT * FROM category WHERE category.idCategory = ?";
			stm = getConnection().prepareStatement(query);
			stm.setInt(1, id);
			ResultSet resultSet = stm.executeQuery();
			resultSet.next();
			int idCategory = resultSet.getInt("idCategory");
			int idParentCategory = resultSet.getInt("idParentCategory");
			String name = resultSet.getString("name");
			String description = resultSet.getString("description");
			int orderNum = resultSet.getInt("orderNum");
			return new Category(idCategory, idParentCategory, name, description, orderNum);
		} catch (SQLException e) {
			//e.printStackTrace();
		} finally {
			try {
				stm.close();
			} catch (SQLException e) {
				//e.printStackTrace();
			}
		}
		
		return null;
	}
	
	
	public void swapCategory(Integer id1, Integer id2) {
		Integer orderNum1 = 0;
		Integer orderNum2 = 0;
		PreparedStatement stm = null;
		ResultSet resultSet = null;
		try {
			String query = "SELECT orderNum FROM category WHERE category.idCategory = ?";
			stm = getConnection().prepareStatement(query);
			stm.setInt(1, id1);
			resultSet = stm.executeQuery();
			resultSet.next();
			orderNum1 = resultSet.getInt("orderNum");
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				stm.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		
		try {
			String query = "SELECT orderNum FROM category WHERE category.idCategory = ?";
			stm = getConnection().prepareStatement(query);
			stm.setInt(1, id2);
			resultSet = stm.executeQuery();
			resultSet.next();
			orderNum2 = resultSet.getInt("orderNum");
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				stm.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		try {
			String query = "UPDATE category SET orderNum = ? WHERE idCategory = ?";
			stm = getConnection().prepareStatement(query);
			stm.setInt(1, orderNum2);
			stm.setInt(2, id1);
			stm.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				stm.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		
		try {
			String query = "UPDATE category SET orderNum = ? WHERE idCategory = ?";
			stm = getConnection().prepareStatement(query);
			stm.setInt(1, orderNum1);
			stm.setInt(2, id2);
			stm.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				stm.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
		
	public void deleteCategory(int id) {
		PreparedStatement stm = null;
		try{
			String query = "DELETE FROM category WHERE idCategory = ?";
			stm = getConnection().prepareStatement(query);
			stm.setInt(1, id);
			stm.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				stm.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	public int getLevelNesting(int idParent) {
		int level = 1;
		PreparedStatement stm = null;
		String query = "SELECT idParentCategory FROM category WHERE idCategory = ?";
		ResultSet resultSet;
		while (idParent != 0) {
			try {
				stm = getConnection().prepareStatement(query);
				stm.setInt(1, idParent);
				resultSet = stm.executeQuery();
				resultSet.next();
				idParent = resultSet.getInt("idParentCategory");
				level++;
			} catch (SQLException e) {
				return 0;
				//e.printStackTrace();
			} finally {
				try {
					stm.close();
				} catch (SQLException e) {
					return 0;
				}
			}
		}
		return level;
	}
}
